/*
 * \brief  Client-side ram session interface
 * \author Norman Feske
 * \date   2006-05-31
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__RAM_SESSION__CLIENT_H_
#define _INCLUDE__RAM_SESSION__CLIENT_H_

#include <ram_session/capability.h>
#include <base/ipc.h>
#include <base/lock.h>

namespace Genode {

	class Ram_session_client : public Ram_session
	{
		private:

			Msgbuf<32> _snd_msg, _rcv_msg;
			Ipc_client _ipc_client;
			Lock       _lock;

		public:

			/**
			 * Constructor
			 */
			explicit Ram_session_client(Ram_session_capability session)
			: _ipc_client(session, &_snd_msg, &_rcv_msg) { }


			/***************************
			 ** Ram session interface **
			 ***************************/

			Ram_dataspace_capability alloc(size_t size)
			{
				Lock::Guard lock_guard(_lock);
				Ram_dataspace_capability result;
				switch (_ipc_client << ALLOC << size << IPC_CALL >> result) {
				case -1: throw Quota_exceeded();
				default: break;
				}
				return result;
			}

			void free(Ram_dataspace_capability ds)
			{
				Lock::Guard lock_guard(_lock);
				_ipc_client << FREE << ds << IPC_CALL;
			}

			int ref_account(Ram_session_capability ram_session)
			{
				Lock::Guard lock_guard(_lock);
				return (_ipc_client << REF_ACCOUNT << ram_session << IPC_CALL);
			}

			int transfer_quota(Ram_session_capability ram_session, size_t amount)
			{
				Lock::Guard lock_guard(_lock);
				return (_ipc_client << TRANSFER_QUOTA << ram_session << amount << IPC_CALL);
			}

			size_t quota()
			{
				Lock::Guard lock_guard(_lock);
				size_t result = 0;
				_ipc_client << QUOTA << IPC_CALL >> result;
				return result;
			}

			size_t used()
			{
				Lock::Guard lock_guard(_lock);
				size_t result = 0;
				_ipc_client << USED << IPC_CALL >> result;
				return result;
			}
	};
}

#endif /* _INCLUDE__RAM_SESSION__CLIENT_H_ */
